package edu.tms.dao;

import java.util.List;
import java.util.Map;

import edu.tms.bean.Teacher;
import edu.tms.crud.JdbcUtil;
import edu.tms.dao.common.CommnonDao;


/**
 * 教师信息表Dao
 * @author 任淋洋
 *
 */
public class TeacherDaoImp implements CommnonDao<Teacher>{
	/**
	 * 保存内容
	 * 返回该条记录的自增长值（id）
	 * @param value
	 * @throws Exception
	 */
	@Override
	public long save(Teacher value) throws Exception {
		String sql="insert into t_teacher values (?,?,?,?,?,?,?,?,?)";
		return JdbcUtil.update(sql,
				value.getTchId(),
				value.getTchName(),
				value.getTchSex(),
				value.getTchQq(),
				value.getTchPhone(),
				value.getTchEmail(),
				value.getCreateTime(),
				value.getTchType(),
				value.getTchState());
	}
	/**
	 * 修改内容
	 * @param value
	 * @throws Exception
	 */
	@Override
	public int update(Teacher value) throws Exception {
		Teacher tea = (Teacher)value;
		String sql="update t_teacher set tch_id=?,tch_name=?,tch_sex=?,tch_qq=?,tch_phone=?,tch_email=?,create_time=?,tch_type=?,tch_state=? where tch_id=?";
		return JdbcUtil.update(sql,tea.getTchId(),tea.getTchName(),tea.getTchSex(),tea.getTchQq(),tea.getTchPhone(),tea.getTchEmail(),
			tea.getCreateTime(),tea.getTchType(),tea.getTchState(),tea.getTchId());
	}
	
	/**
	 * 删除内容
	 * 相关联的表：
	 * 角色信息表t_character，教师表 t_teacher，班级表t_class
	 * 寝室表t_dorm， 课程信息表t_course，作业信息表t_homework， 审核表t_review
	 * 信息记录表 t_message，测评信息表t_assessment_info，学生测评表t_evaluating
	 * 登录日志表t_login_log，操作历史表t_operation ..等等
	 * @param value
	 * @throws Exception
	 */
	@Override
	public int delete(Teacher value) throws Exception {
		Teacher tea = (Teacher)value;
		String sql="delete  t_teacher,t_course,t_homework,t_message,t_assessment_info "
		+"from t_teacher "
		+"left join t_course on t_teacher.tch_id=t_course.tch_id "
		+"left join t_homework on t_teacher.tch_id=t_homework.tch_id "
		+"left join t_message on t_teacher.tch_id=t_message.tch_id "
		+"left join t_assessment_info on t_teacher.tch_id=t_assessment_info.tch_id "
		+"where t_teacher.tch_id=?";
		return JdbcUtil.update(sql,tea.getTchId());
	}
	
	/**
	 * 查找所有的内容
	 * @param pageNo 第几页
	 * @param pageSize 每页数字
	 * @return 
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Teacher> findAll(int pageNo, int pageSize) throws Exception {
		String sql="SELECT * FROM t_teacher limit ?,?";
		List<Teacher> list= JdbcUtil.query(Teacher.class,sql,(pageNo-1)*pageSize,pageSize);
		return list;
	}
	/**
	 * 根据选择的班级查询有那些上课的老师
	 * @throws Exception 
	 */
	public Teacher findtch(String classinfo,String courseinfo) throws Exception{
		String sql=" SELECT tch.tch_name FROM t_course tc,t_class_info tci,t_class_course tcc,t_teacher tch ";
			sql+=" WHERE tcc.course_id=tc.course_id AND tcc.class_id=tci.class_id "
			+ "AND tc.tch_id=tch.tch_id AND tci.class_name=? AND tc.course_name=?";
		return (Teacher) JdbcUtil.uniqueBean(Teacher.class, sql, classinfo,courseinfo);
	}
	/**
	 * 根据条件查找
	 * @param Key
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Teacher> findAll(Object key, int pageNo, int pageSize) throws Exception {
		String sql="select * from t_teacher where tch_id=? or tch_name=? or tch_qq=? or tch_email=? or tch_phone=? or tch_type=? or tch_state=? limit ?,?";
		return JdbcUtil.query(Teacher.class,sql,key,key,key,key,key,key,key,(pageNo-1)*pageSize,pageSize);
	}
	
	
	/**
	 * String 对应  数据库 列名（comment)
	 * Object 对应相应列名值
	 * 返回的是所查找的类中的数据。方便EXCEL导出
	 */
	@Override
	public List<Map<String,Object>> getMapList() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * 根据id查询
	 */
	@Override
	public Teacher findByKey(Object key) throws Exception {
		String sql="select * from t_teacher where tch_id=? ";
		return (Teacher)JdbcUtil.uniqueBean(Teacher.class,sql,key);
	}
	


}



